home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1996 #15
/
Monster Media Number 15 (Monster Media)(July 1996).ISO
/
prog_c
/
kbstf10.zip
/
KBSTUFF.C
next >
Wrap
Text File
|
1996-01-31
|
9KB
|
224 lines
#ifndef __TINY__
#error Must be compiled in tiny model
#endif
#define NULL 0
#if __STDC__
#define _Cdecl
#else
#define _Cdecl cdecl
#endif
void _Cdecl __cli__ (void);
void _Cdecl __sti__ (void);
void _Cdecl __int__(int interruptnum);
extern unsigned _Cdecl _psp;
/* *********************************************************************** */
/* This program stuffs the standard keyboard buffer, which is a circu- */
/* lar buffer of 2-bytes wors */
/* This 2-bytes words are in general formed by the ASCII translation in */
/* the low byte and the scan code in the high byte. Extended characters */
/* could have an ASCII translation of 0 or E0 (for the extended 101/102 */
/* keys keyboards) or F0 (other extended chars). You can inspect keys */
/* stored in the keyboard buffer from the standard INT 9h handler with */
/* the program SEEKBBUF.COM (with Turbo-c source). */
/* */
/* The way to use KBSTUFF is simple for c programmers. */
/* syntax: */
/* KBSTUFF [/R] <what to stuff> */
/* /R Option. If present must be the first parameter, other- */
/* wise is interpreted as two characters to store. */
/* Reset the keyboard's buffer. */
/* <what to stuff> are the characters to store in the buffer. */
/* Blanks and tabs are generally skipped, but you can use */
/* a C-type string, as, for example, */
/* "pippo a:\r" */
/* "\r\n\t\aError in C:\\work\r\n" */
/* "\76\xFDThis are octal and hexadecimal notation\?" */
/* \OOO and \xHHH are accepted as for ANSI C, but if the */
/* value of FFh is exceeded only the low byte is conside- */
/* red and the high is stripped. */
/* With this "normal" use only ASCII translation are sto- */
/* red, without scan code, as the keys would have been ge- */
/* nerated with the ALT-key+Keypad numbers combination. */
/* My extensions to C-string allow to store extended keys */
/* with a low byte of 0. */
/* To store the word 6800h, for example, many methods can */
/* be used: */
/* "\0\x68","\ex68" (where the escape sequence \e is an */
/* extension to the standard C-string syntax), "\0h" or */
/* "\eh" (where ascii('h')=0x68) */
/* The x is used as an escape character for the \e escape */
/* sequence, so if 7800h(=<ALT 1>) has to be stored can be */
/* used: */
/* "\ex78","\0\x78","\0x" or "\exx" */
/* */
/* */
/* (c) 1996 by Luigi Mancinelli */
/* manci@alpha.science.unitn.it */
/* */
/* Note: If the program has to be recompiled, the tiny model has to be */
/* used and the resulting .exe has to be converted in .com (with the DOS */
/* utility EXE2BIN for example) or the program doesn't work. */
/* */
/* *********************************************************************** */
#define MK_FP(seg,ofs) ((void far *) \
(((unsigned long)(seg) << 16) | (unsigned)(ofs)))
#define _toupper(c) ((c)&0xDF)
#define putchar(c) (_DL=c,_AH=02,__int__(0x21))
#define putbackslash() (_DL='\\',_AH=02,__int__(0x21))
void putstring(char *p)
{ unsigned int i=0;
if (p!=NULL)
while (p[i]!=0) putchar(p[i++]);
}
#define FULL 1
#define STRING 2
#define EXTCHAR 4
char Flags=0;
unsigned int p,Word;
unsigned char Ch,c;
unsigned int far *kbdhead = MK_FP(0x40,0x1A);
unsigned int far *kbdtail = MK_FP(0x40,0x1C);
unsigned int far *kbdbufstart = MK_FP(0x40,0x80);
unsigned int far *kbdbufend = MK_FP(0x40,0x82);
char OutString[0x100];
unsigned int l=0;
char *Mess="Stuffed ...\"";
char *HexDig="0123456789ABCDEF";
/* *************************** */
unsigned char *CmdStr=(unsigned char *) 0x80;
int i=1;
void SkipBlanks(void) {
while ((CmdStr[i]==' ')||(CmdStr[i]==9)) i++;
}
/* *************************** */
int GetHexByte(void)
{
register c;
unsigned int j=0;
Ch=0;
while((j++<3)&&
( (((c=CmdStr[i])>='0')&&(c<='9')) ||
(((c=_toupper(c))>='A')&&(c<='F')) ) ) {
if(c>='A') c-=('A'-10); else c-='0';
Ch=(Ch<<4)+c; i++;
}
}
main(){
/*
unsigned int DS=((unsigned long)((char far *)(&CmdStr)))>>16;
if(DS==_psp) {
*/
/* is .COM file */
/* this program must be compiled in tiny model and then */
/* converted in .COM with exe2bin or it doeSn't work. */
{
putstring(Mess); /* printf("Stuffed...\""); */
p=CmdStr[0];
if(CmdStr[p+1]!=0x0D) CmdStr[p+1]=0x0D;
SkipBlanks();
if((CmdStr[i]=='/')&&(_toupper(CmdStr[i+1])=='R')&&
(((i+2)>p)||(CmdStr[i+2]==' ')||
(CmdStr[i+2]==9)||(CmdStr[i+2]=='"')) ) {
__cli__();
*kbdtail=*kbdhead;
__sti__();
i+=2; SkipBlanks();
}
while (((Ch=CmdStr[i++])!=0x0D)&&((Flags & FULL)==0)) {
if((Ch=='"')&& (Flags & STRING)) Flags&=~STRING;
else {
if(Ch=='"') {
register j=i;
Ch=CmdStr[i++];
while ((CmdStr[j]!=0x0D)
&&((CmdStr[j]!='"')||(CmdStr[j-1]=='\\'))) j++;
if (CmdStr[j]==0x0D) break; else Flags|=STRING;
}
if((Flags & STRING)&&(Ch=='\\')) {
Ch=CmdStr[i++];
if((Ch>='0')&&(Ch<='7')) {
register j=1; /* register c; */
Ch-='0';
while((j++<3)&&(((c=CmdStr[i])>='0')&&(c<='7')) )
{ Ch=(Ch<<3)+(c-'0'); i++; }
if(Ch==0) Flags|=EXTCHAR;
}
else switch (Ch) {
/* My extension to standard C-string syntax */
case 'e':Ch=0;
if(CmdStr[i]=='x') {
Flags|=EXTCHAR;
if(CmdStr[++i]=='x') { Ch='x'; i++; }
else GetHexByte();
}
break;
/* Normal C-string syntax */
case 'a':Ch=7; break;
case 'b':Ch=8; break;
case 'f':Ch=0x0C; break;
case 'n':Ch=0x0A; break;
case 'r':Ch=0x0D; break;
case 't':Ch=0x09; break;
case 'v':Ch=0x0B; break;
case 'X':
case 'x':GetHexByte();
break;
case '\'':
case '\?':
case '"':
case '\\':break;
}
}
if(Ch==0) Flags|=EXTCHAR;
else {
__cli__();
p=*kbdtail+2;
if(p==*kbdbufend) p=*kbdbufstart;
if(p!=*kbdhead) {
if(Flags & EXTCHAR) {
Word=((unsigned int) Ch)<<8;
OutString[l++]='\\'; OutString[l++]='e';
if(Ch=='x') OutString[l++]='x';
}
else Word=Ch & 0xFF;
if(Ch=='\\') OutString[l++]='\\';
else if((Ch>=32)&&(Ch!=127)&&(Ch!=255)) OutString[l++]=Ch;
else {
if((Flags & EXTCHAR)==0) OutString[l++]='\\';
OutString[l++]='x';
OutString[l++]=HexDig[Ch>>4];
OutString[l++]=HexDig[Ch& 0xF];
}
*((int far *) (((long)0x400000)|((long)*kbdtail)) ) = Word;
*kbdtail=p;
} else Flags|=FULL;
__sti__();
Flags&=~EXTCHAR;
}
}
if ((Flags & STRING)==0) SkipBlanks();
}
OutString[l++]='"'; OutString[l++]='\r'; OutString[l++]='\n';
putstring(OutString);
}
/* } */
}